package com.platform.manager.action.system;

import com.platform.common.mybatis.Page;
import com.platform.common.mybatis.context.ContextHolder;
import com.platform.common.util.JsonUtil;
import com.platform.domain.model.CodeClass;
import com.platform.domain.model.Function;
import com.platform.domain.result.DataGirdData;
import com.platform.domain.result.ResultData;
import com.platform.manager.action.BaseAction;
import com.platform.service.CodeClassService;
import org.apache.struts2.convention.annotation.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: wangyanginfo
 * Date: 12-11-7
 * Time: 下午2:36
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/system")
@Service("codeClassAction")
@ParentPackage("jsonPackage")
public class CodeClassAction extends BaseAction {

    @Resource
    private CodeClassService codeClassService;

    /**
     * 查询字典类型信息列表
     *
     * @return
     */
    @Action(value = "queryCodeClass", interceptorRefs = @InterceptorRef("pageStack"),
            results = {@Result(name = "success", type = "json", params = {"root", "dataGirdData"})})
    public String queryCodeClass() {
        try {
            // 查询符合条件的用户列表
            List<CodeClass> list = codeClassService.select(null);
            dataGirdData = new DataGirdData();
            dataGirdData.setRows(list);
            // 获取翻页参数
            Page page = ContextHolder.getPage();
            dataGirdData.setTotal(page.getTotalResult());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 保存字典类型列表
     *
     * @return
     */
    @Action(value = "saveCodeClass", results = {@Result(name = "success", type = "json", params = {"root", "result"})})
    public String saveCodeClass() {
        try {
            if (jsonStr != null) {
                print("前台发送的JSON数据：" + jsonStr);
                Map<String, List> maps = JsonUtil.jsonToMap(jsonStr, CodeClass.class);
                List inserted = maps.get("inserted");
                List updated = maps.get("updated");
                List deleted = maps.get("deleted");

                // 保存未插入、更新、删除成功的的用户登录名
                StringBuffer buffer = new StringBuffer();

                if (inserted != null) {
                    for(Object obj : inserted){
                        CodeClass codeClass = (CodeClass) obj;
                        int resInt = codeClassService.insert(codeClass);
                        if (resInt != 1) {
                            buffer.append(" [").append(codeClass.getClassId()).append("]");
                        }
                    }
                }

                if (updated != null) {
                    for(Object obj : updated){
                        CodeClass codeClass = (CodeClass) obj;
                        int resInt = codeClassService.update(codeClass);
                        if (resInt != 1) {
                            buffer.append(" [").append(codeClass.getClassId()).append("]");
                        }
                    }
                }

                if (deleted != null) {
                    for(Object obj : deleted){
                        CodeClass codeClass = (CodeClass) obj;
                        int resInt = codeClassService.delete(codeClass);
                        if (resInt != 1) {
                            buffer.append(" [").append(codeClass.getClassId()).append("]");
                        }
                    }
                }


                result = new ResultData();
                if (buffer.length() == 0) {
                    result.setSuccess(true);
                    result.setMessage("操作成功！");
                } else {
                    result.setSuccess(false);
                    result.setMessage("操作失败！\n以下字典类型操作失败！\n[类型ID]：" + buffer.toString());
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }
}
